#! /usr/bin/env node
import inquirer from "inquirer";
import { PROMPT, ENUMS } from "./constants";
/** 提示输入内容 */
export const prompts = [
  {
    type: "input",
    name: PROMPT.NAME,
    message: "项目名称：",
  },
  {
    type: "list",
    name: PROMPT.TEMPLATE,
    message: "请选择生成模板类型：",
    choices: [
      ENUMS[PROMPT.TEMPLATE].nuxt,
      ENUMS[PROMPT.TEMPLATE].sdk,
      ENUMS[PROMPT.TEMPLATE].vue,
      ENUMS[PROMPT.TEMPLATE].uniapp,
    ],
  },
];
/** 获取用户选择配置 */
export async function getPrompt() {
  try {
    const commandRes = await inquirer.prompt(prompts);
    const map = prompts.reduce(
      (prev, cur) => {
        prev[cur.name] = cur.type;
        return prev;
      },
      {} as { [key: string]: string }
    );
    for (const key in commandRes) {
      if (Object.hasOwnProperty.call(commandRes, key)) {
        const type = map[key];
        if (type === "select") {
          commandRes[key] =
            commandRes[key].toUpperCase() === "Y" ? true : false;
        }
      }
    }
    if (!commandRes[PROMPT.NAME]) {
      commandRes[PROMPT.NAME] = "project-name";
    }
    return commandRes;
  } catch (error) {
    console.error(error);
    process.exit(0);
  }
}
